﻿namespace com.oakwoodsys.ConsultantCareDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using com.oakwoodsys.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = ConsultantCareDataContext;

    using ReferenceObject = Recruiter;

    public class RecruiterDao : BaseDao
    {
        #region Fields

        public const string ListName = "recruiters";

        #endregion Fields

        #region Constructors

        public RecruiterDao( )
            : base(ListName)
        {
            CurrentWebUrl = SPContext.Current.Web.Url;
        }

        public RecruiterDao( string currentWebUrl )
            : base(ListName)
        {
            CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public bool ExistsForRecruiter(int id)
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.RecruiterId == id
                                      select o ).SingleOrDefault( );
                return v != null;
            }
        }

        public ReferenceObject Get( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.UniqueId == id
                                      select o ).SingleOrDefault( );
                return v;
            }
        }

        public ReferenceObject GetByRecruiter( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.RecruiterId == id
                                      select o ).SingleOrDefault( );
                return v;
            }
        }

        #endregion Methods

        #region Nested Types

        class Query
        {
            #region Fields

            public const string ByRecruiter = "<Where><Eq><FieldRef Name='occRecruiter' /><Value Type='User'>{0}</Value></Eq></Where>";

            #endregion Fields

            #region Methods

            public static string Get( string query, params object[ ] args )
            {
                return string.Format( query, args );
            }

            #endregion Methods
        }

        #endregion Nested Types
    }
}